﻿
Partial Class WebForm_ManageWorkContent_vSelfRegionType
    Inherits System.Web.UI.UserControl

    Private Function GetRegionType(ByVal value As String) As System.Data.DataTable

        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.AppendFormat("select b.* ")
        sql.AppendFormat("from CodeMapping a inner join CodeMapping b ")
        sql.AppendFormat("on a.TableName+'_'+a.ColumnName+'_'+a.Value=b.TableName ")
        sql.AppendFormat("where a.TableName='CleanPlan' ")
        sql.AppendFormat("and a.ColumnName='ItemGroup' ")
        sql.AppendFormat("and a.Value='{0}' ", value)
        sql.AppendFormat("and a.Active = 'Y' and b.Active = 'Y' ")

        Return dat.GetData(sql)

    End Function

    Public Sub BindgvRegionType()
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.AppendFormat("select * from CodeMapping ")
        sql.AppendFormat("where TableName = 'CleanPlan' ")
        sql.AppendFormat("and ColumnName='ItemGroup' ")
        sql.AppendFormat("and Active='Y' ")

        Me.gvRegionType.DataSource = dat.GetData(sql)
        Me.gvRegionType.DataBind()

    End Sub

    Public Sub Enable(ByVal bool As Boolean)
        Me.gvRegionType.Enabled = bool
    End Sub
   
    Protected Sub lbtnReset_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbtnReset.Click
        For i As Integer = 0 To Me.gvRegionType.Rows.Count - 1
            For j As Integer = 0 To TryCast(Me.gvRegionType.Rows(i).FindControl("ckList"), CheckBoxList).Items.Count - 1
                TryCast(Me.gvRegionType.Rows(i).FindControl("ckList"), CheckBoxList).Items(j).Selected = False
            Next
        Next
    End Sub

    Public Sub BindRegionItem(ByVal planID As String, ByVal OtherRegion As String)

        Dim sql As New MyStringBuilder
        Dim dat As New clsData

        sql.Length = 0

        sql.AppendFormat(" Select * From CleanPlanContent Where PlanID=@PlanId ")
        sql.AddWithValue("PlanId", planID)

        Dim dt As Data.DataTable = dat.GetData(sql)

        If dt.Rows.Count > 0 Then
            For i As Integer = 0 To dt.Rows.Count - 1
                If dt.Rows(i)("ItemGroup") = CType(Me.gvRegionType.Rows(i).FindControl("lblRegionValue"), Label).Text Then
                    'CType(Me.gvRegionType.Rows(i).FindControl("ckList"), CheckBoxList).Items(i).Selected = True
                    For j As Integer = 0 To dt.Rows.Count - 1
                        Dim itemindex As Integer = Convert.ToInt32(dt.Rows(j)("ItemValue").ToString()) - 1
                        CType(Me.gvRegionType.Rows(i).FindControl("ckList"), CheckBoxList).Items(itemindex).Selected = True
                    Next
                End If
            Next
        End If

        TryCast(Me.gvRegionType.FooterRow.FindControl("txtOther"), TextBox).Text = OtherRegion

    End Sub

    Protected Sub gvRegionType_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvRegionType.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then

            Dim dt As New System.Data.DataTable

            dt = Me.GetRegionType(TryCast(e.Row.FindControl("lblRegionValue"), Label).Text)

            If dt.Rows.Count > 0 Then
                For i As Integer = 0 To dt.Rows.Count - 1
                    Dim item As New ListItem
                    item.Text = dt.Rows(i)("Text").ToString()
                    item.Value = dt.Rows(i)("Value").ToString()
                    TryCast(e.Row.FindControl("ckList"), CheckBoxList).Items.Add(item)
                Next
            End If

        End If
    End Sub


#Region "Property"

    Public Property RegionText() As String
        Get
            'If TryCast(Me.gvRegionType.FooterRow.FindControl("rbListOther"), RadioButtonList).Items(0).Selected Then

            If TryCast(Me.gvRegionType.FooterRow.FindControl("txtOther"), TextBox).Text <> "" Then
                Return TryCast(Me.gvRegionType.FooterRow.FindControl("txtOther"), TextBox).Text
            Else
                Return ViewState("RegionText")
            End If

        End Get
        Set(ByVal value As String)
            ViewState("RegionText") = value
        End Set
    End Property


    Public ReadOnly Property OtherRegion() As String
        Get
            Return TryCast(Me.gvRegionType.FooterRow.FindControl("txtOther"), TextBox).Text
        End Get
    End Property

    Public ReadOnly Property RegionView() As Data.DataTable
        Get
            Dim dt As New Data.DataTable

            dt.Columns.Add("ItemGroup")
            dt.Columns.Add("ItemValue")

            For i As Integer = 0 To Me.gvRegionType.Rows.Count - 1
                For j As Integer = 0 To TryCast(Me.gvRegionType.Rows(i).FindControl("ckList"), CheckBoxList).Items.Count - 1
                    If TryCast(Me.gvRegionType.Rows(i).FindControl("ckList"), CheckBoxList).Items(j).Selected Then

                        Dim vrow As Data.DataRow = dt.NewRow()
                        vrow("ItemGroup") = TryCast(Me.gvRegionType.Rows(i).FindControl("lblRegionValue"), Label).Text
                        vrow("ItemValue") = TryCast(Me.gvRegionType.Rows(i).FindControl("ckList"), CheckBoxList).Items(j).Value

                        dt.Rows.Add(vrow)

                    End If
                    'TryCast(Me.gvRegionType.Rows(i).FindControl("rbList"), RadioButtonList).Items(j).Selected = False
                Next
            Next

            ViewState("RegionText") = dt

            Return ViewState("RegionText")
        End Get
    End Property
    Public WriteOnly Property ResetLink() As Boolean
        Set(ByVal value As Boolean)
            Me.lbtnReset.Visible = value
        End Set
    End Property
#End Region

End Class
